package com.golang.api.util;

import cn.hutool.core.util.StrUtil;

/**
 * 统一业务异常
 *
 * @author wanmingyu
 * @create 2020/7/3 5:59 下午
 */
public class ServiceException extends RuntimeException {

  private Integer code;
  private String message;

  @Deprecated
  public ServiceException (String message) {
    super(message);
    this.message = message;
  }

  public ServiceException (Integer code, String message) {
    super(message);
    this.code = code;
    this.message = message;
  }

  /**
   * eg: new ServiceException(ResponseEnum.ERROR)
   *
   * @param statusEnumSupport
   */
  public ServiceException (StatusEnumSupport statusEnumSupport) {
    super(statusEnumSupport.getMessage());
    this.code = statusEnumSupport.getCode();
    this.message = super.getMessage();
  }

  /**
   * eg: new ServiceException(ResponseEnum.ERROR,"失败",new IllegalAccessException())
   *
   * @param statusEnumSupport
   * @param throwable
   */
  public ServiceException (StatusEnumSupport statusEnumSupport, Throwable throwable) {
    super(statusEnumSupport.getMessage(), throwable);
    this.code = statusEnumSupport.getCode();
    this.message = super.getMessage();
  }

  /**
   * eg: new ServiceException(ResponseEnum.ERROR,"返回信息为:{},{}","处理","失败")
   *
   * @param statusEnumSupport
   * @param params
   */
  public ServiceException (StatusEnumSupport statusEnumSupport, Object... params) {
    super(StrUtil.format(statusEnumSupport.getMessage(), params));
    this.code = statusEnumSupport.getCode();
    this.message = super.getMessage();
  }

  public static ServiceException error(String msg){
      return new ServiceException(500,msg);
  }

  /**
   * eg: new ServiceException(500,"返回信息为:{}","失败")
   *
   * @param code
   * @param messageTemplate
   * @param params
   */
  @Deprecated
  public ServiceException (Integer code, String messageTemplate, Object... params) {
    super(StrUtil.format(messageTemplate, params));
    this.code = code;
    this.message = super.getMessage();
  }

  public Integer getCode () {
    return code;
  }

  @Override
  public String getMessage () {
    return message;
  }
}